package com.ruoyi.project.cms.sale.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.client.domain.CmsClient;
import com.ruoyi.project.cms.sale.domian.Sale;
import com.ruoyi.project.cms.sale.service.ISaleService;
import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类名:SaleController
 * 描述:TODO
 * 作者:邓怀广
 * 时间:2020-05-19
 */
@Controller
@RequestMapping("/cms/sale")
public class SaleController extends BaseController {

    private String prefix = "cms/sale";

    @Autowired
    ISaleService saleService;

    @GetMapping()
    public String Sale() {
        return prefix+"/sale";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Sale sale) {
        System.out.println(sale);
        startPage();
        List<Sale> list = saleService.selectSaleList(sale);
        for (Sale sale1: list) {
            System.out.println(sale1);
        }
        return getDataTable(list);
    }

    @GetMapping("/userList")
    @ResponseBody
    public Object userList(ModelMap map) {
        List<User> list = saleService.selectUserList();
        map.addAttribute("userList", list);
        return new AjaxResult(AjaxResult.Type.SUCCESS,"200",list);
    }

    @GetMapping("/clientList")
    @ResponseBody
    public Object clientList(ModelMap map) {
        List<CmsClient> list = saleService.selectClientList();
        map.addAttribute("clients", list);
        return new AjaxResult(AjaxResult.Type.SUCCESS,"200",list);
    }

    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存角色
     */
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public Object addSave(@Validated Sale sale) {
        System.out.println(sale);
        return toAjax(saleService.add(sale));
    }

    /**
     * 修改角色
     */
    @GetMapping("/edit/{sId}")
    public String edit(@PathVariable("sId") Long sId, ModelMap mmap)
    {
        Sale sale = saleService.selectSaleById(sId);
        System.out.println(sale);
        mmap.put("sale", sale);
        return prefix + "/edit";
    }

    @Log(title = "销售管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Sale sale) {
       return toAjax(saleService.updateSale(sale));
    }

    @Log(title = "销售管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(saleService.deleteSaleByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

}
